<?php
    /**
    * Get list of meetings belongs to user
    */
    if (!defined('EXPONENT')) exit('');
    function _manage_meetings_return_empty()
    {
        $ret_object = null;
        $ret_object->total = 0;
        $ret_object->rows=array(); 
        echo json_encode($ret_object);
        return;
    }

    if (exponent_users_isAdmin() ||  exponent_permissions_check(array("administrate","meeting","meeting_delete","meeting_assistant"),exponent_core_makeLocation("profilemodule","_syscore","")))
    {  
        $user_id=0;
        if (isset($_REQUEST['user_id']))
            $user_id = intval($_REQUEST['user_id']);
        if ($user_id ==0)
            _manage_meetings_return_empty();
        else
        {
            // select user
            $cuser = $db->selectObject("user", "id = {$user_id}");
            if (!isset($cuser->id))
            {
                _manage_meetings_return_empty();
            }
            else
            {
                $sql_where = "user_id = {$user_id} ";

                // Build condition for filter
                if (isset($_REQUEST['search']))
                {
                    // get search type
                    // all ; subject; lawyer; mentor
                    $search_type = $_REQUEST['search_type'];
                    $search = $_REQUEST['search'];
                    if ($search_type == "meeting")
                    {
                        $sql_where.=" AND (previous_subject LIKE '%{$search}%' OR new_subject LIKE '%{$search}%' OR meeting_notes LIKE '%{$search}%' OR meeting_place LIKE '%{$search}%'  OR related_lawyers LIKE '%{$search}%')";
                    }
                    elseif ($search_type == "mentor")
                    {
                        $sql_where.=" AND (mentor_location LIKE '%{$search}%' OR mentor_language LIKE '%{$search}%' OR mentor_required LIKE '%{$search}%')";
                    }
                    elseif ($search_type == "all")
                    {
                        $sql_where.=" AND (mentor_location LIKE '%{$search}%' OR mentor_language LIKE '%{$search}%' OR mentor_required LIKE '%{$search}%' OR previous_subject LIKE '%{$search}%' OR new_subject LIKE '%{$search}%' OR meeting_notes LIKE '%{$search}%' OR meeting_place LIKE '%{$search}%'  OR related_lawyers LIKE '%{$search}%')";
                    }
                }
                    
                if (isset($_REQUEST['datefrom']) && $_REQUEST['datefrom']!="")
                {
                    $date_from=strtotime($_REQUEST['datefrom']);
                    $sql_where.=" AND (meeting_date>={$date_from})";
                }
                
                if (isset($_REQUEST['dateto']) && $_REQUEST['dateto']!="")
                {
                    $date_to=strtotime($_REQUEST['dateto']);
                    $sql_where.=" AND (meeting_date<={$date_to})";
                }
                
                if (isset($_REQUEST['status']))
                {
                    $status = intval($_REQUEST['status']);
                    if (!(($status >=-1) && ($status <=3)))
                    {
                        $status=-1;
                    }
                    if ($status > -1)
                    {
                        $sql_where.=" AND (approve_state = {$status})";
                    }
                }
                
                $page=1;
                if (isset($_REQUEST['page']))
                    $page=intval($_REQUEST['page']);
                $pageSize=10;

                if (isset($_REQUEST['rows']))
                    $pageSize=intval($_REQUEST['rows']);
                $start=($page-1)*$pageSize;

                // sort
                $sort="";
                if (isset($_REQUEST['sortName'])){
                    $sort = " ORDER BY {$_REQUEST['sortName']} {$_REQUEST['sortOrder']}";
                }

                $data=$db->selectObjects('profilemodule_meeting',"{$sql_where} {$sort} LIMIT {$start},{$pageSize}");

                // preprocess some data
                foreach ($data as $k=>$v){
                    $v->meeting_date_text = date('d/m/Y H:i:s',$v->meeting_date);
                    if ($v->approve_state == 0)
                        $v->approve_date_text ="";
                    else
                        $v->approve_date_text = date('d/m/Y H:i:s',$v->approve_date);
                        
                    $v->request_date_text = date('d/m/Y H:i:s',$v->request_date);
                    $v->last_update_text = date('d/m/Y H:i:s',$v->last_update);
                }

                $ret_object = null;
                $ret_object->total = $db->countObjects('profilemodule_meeting',"{$sql_where}");
                $ret_object->rows = $data; 
                echo json_encode($ret_object);
            }
        }
    }

?>